package com.easy.web.controller.merchants;

import com.easy.common.core.R;
import com.easy.common.utils.ServletUtils;
import com.easy.merchants.pojo.HouseInformation;
import com.easy.merchants.pojo.dto.HouseDto;
import com.easy.merchants.service.MerchantsService;
import com.easy.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import static com.easy.common.constant.HttpStatus.UNAUTHORIZED;
import static com.easy.common.utils.RedisConstants.LOGIN_USER_KEY;

/**
 * 商家 自助 控制器
 *
 * @author RuoYu
 * @date 2022/12/07
 */
@RestController
@RequestMapping("/house")
@Api(tags = "商家 控制器")
public class MerchantsController {

    @Autowired
    private MerchantsService merchantsService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;



    /**
     * 发布房屋信息
     *
     * @param houseDto 房子dto
     * @return {@link R}
     */
    @PostMapping
    @ApiOperation(value = "发布房屋信息")
    @ApiImplicitParam(name = "houseDto", value = "房子信息", required = true)
    public R releaseHouseInfo(@RequestBody HouseDto houseDto) {

        String token = LOGIN_USER_KEY + ServletUtils.getRequest().getHeader("authorization");
        String userType = stringRedisTemplate.opsForHash().get(token, "type") + "";
        if (userType.equals("1")) {
            merchantsService.release(houseDto);
          return R.success();
        }
        ServletUtils.getResponse().setStatus(UNAUTHORIZED);
        return R.error(UNAUTHORIZED + "");
    }

    /**
     * 修改房屋信息
     *
     * @param houseDto 房子dto
     * @return {@link R}
     */
    @PutMapping
    @ApiOperation(value = "修改房屋信息")
    @ApiImplicitParam(name = "houseDto", value = "房子信息", required = true)
    public R modifyHouseInfo(@RequestBody HouseDto houseDto) {


        String token = LOGIN_USER_KEY + ServletUtils.getRequest().getHeader("authorization");
        String userType = stringRedisTemplate.opsForHash().get(token, "type") + "";
        if (userType.equals("1")) {
            merchantsService.modify(houseDto);
            return R.success();
        }
        ServletUtils.getResponse().setStatus(UNAUTHORIZED);
        return R.error(UNAUTHORIZED + "");
    }

    /**
     * 逻辑删除房屋信息
     *
     * @param id 房子信息主键id
     * @return {@link R}
     */
    @DeleteMapping
    @ApiOperation(value = "逻辑删除房屋信息")
    @ApiImplicitParam(name = "id", value = "房子信息主键id", required = true)
    public R deleteHouseInfo(Long id) {

        String token = LOGIN_USER_KEY + ServletUtils.getRequest().getHeader("authorization");
        String userType = stringRedisTemplate.opsForHash().get(token, "type") + "";
        if (userType.equals("1")) {
            merchantsService.delete(id);
            return R.success();
        }
        ServletUtils.getResponse().setStatus(UNAUTHORIZED);
        return R.error(UNAUTHORIZED + "");
    }

    /**
     * 物理删除房屋信息
     *
     * @param id 房子信息主键id
     * @return {@link R}
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "物理删除房屋信息")
    @ApiImplicitParam(name = "id", value = "房子信息主键id", required = true)
    public R deletePhysicalHouseInfo(Long id) {


        String token = LOGIN_USER_KEY + ServletUtils.getRequest().getHeader("authorization");
        String userType = stringRedisTemplate.opsForHash().get(token, "type") + "";
        if (userType.equals("1")) {
            merchantsService.physicalDeletion(id);
            return R.success();
        }
        ServletUtils.getResponse().setStatus(UNAUTHORIZED);
        return R.error(UNAUTHORIZED + "");
    }

    @Autowired
    private UserService userService;

    /**
     * 更新房子状态(起售停售)
     *
     * @param id     房子信息主键id
     * @param status 状态
     * @return {@link R}
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新房子状态(起售停售)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "房子信息主键id", required = true),
            @ApiImplicitParam(name = "status", value = "状态 0：停止 1：起售", required = true)
    })
    public R updateHouseStatus(Long id, String status) {

        String token = LOGIN_USER_KEY + ServletUtils.getRequest().getHeader("authorization");
        String userType = stringRedisTemplate.opsForHash().get(token, "type") + "";
        Long houseOwner = Long.valueOf(stringRedisTemplate.opsForHash().get(token, "Id") + "");
        if (userType.equals("1")) {
            HouseInformation houseInformation = new HouseInformation();
            houseInformation.setId(id);
            houseInformation.setHouseOwner(houseOwner);
            houseInformation.setStatus(status);
            merchantsService.startAndStop(houseInformation);
            return R.success();
        }
        ServletUtils.getResponse().setStatus(UNAUTHORIZED);
        return R.error(UNAUTHORIZED + "");
    }


}
